package com.key.puzzlemodel.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View


class ColorPicker @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private var xCenter = 0f
    private var yCenter = 0f

    private var lastX = 0f
    private var lastY = 0f
    private lateinit var tempBitmap: Bitmap
    private lateinit var pickView: View
    var color = Color.RED

    private var actionUpListener: (() -> Unit)? = null

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(pickView.width, pickView.height)
    }

    fun setPickView(view: View) {
        this.pickView = view
    }

    fun setTempBitmap(bitmap: Bitmap) {
        this.tempBitmap = bitmap
    }

    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        val oval = RectF()
        oval.left = xCenter - 150f
        oval.top = yCenter - 150f
        oval.right = 150f * 2 + (xCenter - 150f)
        oval.bottom = 150f * 2 + (yCenter - 150f)

        var smallCirclePaint = Paint()
        smallCirclePaint.isAntiAlias = true
        smallCirclePaint.isDither = true
        smallCirclePaint.strokeWidth = 70f
        smallCirclePaint.color = color
        smallCirclePaint.style = Paint.Style.STROKE

        var smallPaint = Paint()
        smallPaint.isAntiAlias = true
        smallPaint.isDither = true
        smallPaint.color = Color.BLACK
        smallPaint.style = Paint.Style.FILL
        canvas.drawArc(oval, -90f, 360f, false, smallCirclePaint)
        canvas.drawCircle(xCenter, yCenter, 5F, smallPaint)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                lastX = event.rawX
                lastY = event.rawY
            }
            MotionEvent.ACTION_MOVE -> {
                var currentX = event.rawX
                var currentY = event.rawY
                var disX = currentX - lastX
                var disY = currentY - lastY
                xCenter += disX
                yCenter += disY
                if (xCenter > width - 1) {
                    xCenter = width.toFloat() - 1
                } else if (xCenter < 0) {
                    xCenter = 0f
                }
                if (yCenter > height - 1) {
                    yCenter = height.toFloat() - 1
                } else if (yCenter < 0) {
                    yCenter = 0f
                }
                lastX = currentX
                lastY = currentY
                color = tempBitmap.getPixel(xCenter.toInt(), yCenter.toInt())
                postInvalidate()

            }
            MotionEvent.ACTION_UP -> {
                actionUpListener?.invoke()
            }

        }
        return true

    }

    fun setOnActionUpListener(listener: (() -> Unit)) {
        actionUpListener = listener
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        xCenter = width / 2f
        yCenter = height / 2f
        color = tempBitmap.getPixel(xCenter.toInt(), yCenter.toInt())
    }
}